package com.example.springboot1.draw;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author ckj
 * @description TODO
 * @date 2023-04-06 15:41
 */


public class DrawDemo {
    public static void main(String[] args) throws IOException {
        draw();
    }

    public  static void draw() throws IOException {
        //创建图片对象
        BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
        //基于图片对象打开绘图
        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.RED);
        g2.setStroke(new BasicStroke(2));
        g2.draw(new Ellipse2D.Double(50, 50, 100, 100));

        g2.setColor(Color.BLUE);
        g2.setFont(new Font("Arial", Font.BOLD, 24));
        g2.drawString("Hello, world!", 100, 150);

        g2.dispose();
        ImageIO.write(image, "png", new File("a.png"));

    }

    public static void draw2() throws IOException {
        //创建图片对象
        BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
        //基于图片对象打开绘图
        Graphics2D graphics = image.createGraphics();
        //消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 前置可以进行线条粗细、字体大小、画笔颜色等属性的设置
        // 设置画笔颜色
        graphics.setColor(Color.GRAY);
        // 设置画笔粗细
        Stroke stroke=new BasicStroke(1.0f);//设置线宽为2.0
        graphics.setStroke(stroke);
        // 设置字体大小
        graphics.setFont(new Font("仿宋",Font.BOLD,10));
        // 绘制文字 content-内容  xy坐标
        // 文字画入画布中
        graphics.drawString("测试", 5,1);
        Rectangle2D back = new Rectangle2D.Double(20,30,300,40);
        graphics.fill(back);
        // 直接放入图形对象
        graphics.draw(back);
        // 可以写入本地文件（文件名无路径默认/src下）
        //保存图片 JPG表示保存格式
//        ImageIO.write(image,"JPEG",new FileOutputStream("a.jpg"));
        ImageIO.write(image, "png", new File("abcd.png"));
    }
}
